package com.gis.visualization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gis.visualization.config.result.R;
import com.gis.visualization.entity.HistorySummary;
import com.gis.visualization.service.HistorySummaryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 人物或事件的历史结论 前端控制器
 * </p>
 *
 * @author hqz
 * @since 2021-02-23
 */
@RestController
@RequestMapping("/visualization/historysummary")
//@CrossOrigin
public class HistorySummaryController {

    @Autowired
    private HistorySummaryService summaryService;


    @ApiOperation("获取历史人物总结")
    @GetMapping("getFigureSummary/{figureId}")
    public R getFigureSummary(@PathVariable String figureId) {
        HistorySummary figureSummary = summaryService.getById(figureId);
        if(figureSummary == null){
            figureSummary = new HistorySummary();
        }
        return R.ok().data("figureSummary", figureSummary);
    }

    @ApiOperation("更新历史人物总结")
    @PostMapping("saveOrUpdateSummary")
    public R saveOrUpdateSummary(@RequestBody HistorySummary summary) {
        QueryWrapper<HistorySummary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", summary.getId());
        int count = summaryService.count(queryWrapper);
//        能查到数据就是更新，否则是添加
        if (count > 0) {
            summaryService.updateById(summary);
            return R.ok().message("历史人物总结更新成功");
        }else {
            summaryService.save(summary);
            return R.ok().message("历史人物总结添加成功");
        }

    }
}

